package com.zdl.interestingapp.data;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author zdl
 * @date 2018/4/23 11:02
 * email zdl328465042@163.com
 * description
 */
public class ApiClient {
    private static Retrofit.Builder retrofitBuilder = null;
    public static Retrofit.Builder retrofitBuilder(){
        if (null == retrofitBuilder){
            OkHttpClient.Builder builder = new OkHttpClient.Builder();

            //缓存机制

            //公共参数
            Interceptor addRequestParameterInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request originalRequest = chain.request();
                    Request request;
                    String method = originalRequest.method();
                    Headers headers = originalRequest.headers();
                    HttpUrl modifiedUrl = originalRequest.url().newBuilder()
                            //Provide your custom parameter here
                            .addQueryParameter("token", "")
                            .build();
                    request = originalRequest.newBuilder().url(modifiedUrl).build();
                    return chain.proceed(request);
                }
            };
//            builder.addInterceptor(addRequestParameterInterceptor);

            //设置头
            Interceptor addHeaderInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request originalRequest = chain.request();
                    Request request = originalRequest.newBuilder()
                            .header("Content-type", "application/json")
                            .header("Accept", "application/json")
                            .method(originalRequest.method(), originalRequest.body())
                            .build();
                    return chain.proceed(request);
                }
            };
            builder.addInterceptor(addHeaderInterceptor);

            //设置超时
            builder.connectTimeout(10, TimeUnit.SECONDS);
            builder.readTimeout(10, TimeUnit.SECONDS);
            builder.writeTimeout(10, TimeUnit.SECONDS);
            //错误重连
            builder.retryOnConnectionFailure(true);

            //设置结束，进行build，不然设置不生效
            OkHttpClient okHttpClient = builder.build();
            retrofitBuilder = new Retrofit.Builder()
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        }

        return retrofitBuilder;
    }
}
